import {
  createInbizSchema,
  createBaseSchema,
  createComponentStyleSchema,
  createDecoratorStyleSchema,
  createModelSchema,
  createValidatorSchema,
  createBizProps,
  createComponentLayout,
  createControllerSchema,
} from '@inbiz/setters';

export const tabsData = [
  {
    id: 'eformMemberSelectDepartment',
    isEdit: true,
    isHide: true,
    isDel: false,
    title: 'EformMemberSelect.lang.personTabs.department',
    titleDisplayFormat: 'department',
    memberKey: 'department',
  },
  {
    id: 'eformMemberSelectPosition',
    isEdit: true,
    isHide: true,
    isDel: false,
    title: 'EformMemberSelect.lang.personTabs.position',
    titleDisplayFormat: 'position',
    memberKey: 'position',
  },
  {
    id: 'eformMemberSelectUserGroup',
    isEdit: true,
    isHide: true,
    isDel: false,
    title: 'EformMemberSelect.lang.personTabs.userGroup',
    // dataRange: {
    //   type: 'all',
    //   data: [],
    // },
    memberKey: 'group',
  },
  {
    id: 'eformMemberSelectUser',
    isEdit: true,
    isHide: true,
    isDel: false,
    title: 'EformMemberSelect.lang.personTabs.user',
    positionSelect: false,
    // departmentUserGroup: ['department'],
    // departmentSelector: [],
    departmentDisplayFormat: 'name',
    ifUserDepartmentSelected: true,
    ifUserGroupSelected: false,
    userGroupFilterRule: 'selected',
    userGroups: [],
    // userGroupSelector: {
    //   type: 'all',
    //   data: [],
    // },
    userGroupDisplayFormat: 'name',
    memberKey: 'user',
  },
];

export const commonProps = {
  title: {
    status: true,
    value: 'EformMemberSelect.title',
  },
};

export const defaultBizProps = {
  collectivization: false,
  departmentFilteringRules: 'selected',
  departments: '',
  groupFilteringRules: 'selected',
  groups: '',
};

export const defaultProps = {
  tabs: tabsData,
  pageSize: 10,
  multiple: false,
  multipleMaxHeight: 200,
  value: [],
  modalTitle: 'EformMemberSelect.title',
  placeholder: 'EformMemberSelect.lang.searchUserNameToSelect',
};

// Shema
export const propsSchema = createInbizSchema({
  attribute: {
    ...createBaseSchema({
      placeholder: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'MultilingualInput',
      },
      modalTitle: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'MultilingualInput',
      },
      tabs: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'DataSource',
        // 根据配置显示
        'x-component-props': {
          config: {
            // 标题
            title: true,
            hideAddBtn: true,
            departmentTitleData: [
              {
                value: 'department',
                label: 'EformMemberSelect.lang.personTabs.department',
              },
              {
                value: 'departments',
                label: 'EformMemberSelect.lang.personTabs.departments',
              },
            ],
            positionTitleData: [
              {
                value: 'position',
                label: 'EformMemberSelect.lang.personTabs.position',
              },
              {
                value: 'departmentPosition',
                label: 'EformMemberSelect.lang.personTabs.departmentPosition',
              },
              {
                value: 'departmentsPosition',
                label: 'EformMemberSelect.lang.personTabs.departmentsPosition',
              },
            ],
            departmentUserGroup: [
              {
                value: 'department',
                label: 'EformMemberSelect.lang.personTabs.department',
              },
              {
                value: 'userGroup',
                label: 'EformMemberSelect.lang.personTabs.userGroup',
              },
            ],
            userDepartmentTitleData: [
              {
                value: 'name',
                label: 'EformMemberSelect.lang.name',
              },
              {
                value: 'nameUsername',
                label: 'EformMemberSelect.lang.nameUsername',
              },
              {
                value: 'departmentName',
                label: 'EformMemberSelect.lang.departmentName',
              },
              {
                value: 'positionName',
                label: 'EformMemberSelect.lang.position_name',
              },
            ],
            userGroupTitleData: [
              {
                value: 'name',
                label: 'EformMemberSelect.lang.name',
              },
              {
                value: 'nameUsername',
                label: 'EformMemberSelect.lang.nameUsername',
              },
              {
                value: 'userGroupName',
                label: 'EformMemberSelect.lang.userGroupName',
              },
              {
                value: 'positionName',
                label: 'EformMemberSelect.lang.position_name',
              },
            ],
            userGroupFilterRuleData: [
              {
                value: 'selected',
                label: 'EformMemberSelect.lang.selectedGroups',
              },
              {
                value: 'exceptSelected',
                label: 'EformMemberSelect.lang.exceptSelected',
              },
            ],
          },
          // 新建时表单默认值
          default: {
            title: '',
          },
          defaultValue: tabsData,
        },
      },
      multiple: {
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
        enum: [true, false],
        'x-reactions': [
          {
            target: 'form.attribute.base.x-component-props.defaultValue',
            effects: ['onFieldInputValueChange'],
            fulfill: {
              state: {
                value: [],
              },
            },
          },
        ],
      },
      multipleMaxHeight: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'Input',
        'x-reactions': [
          {
            dependencies: ['.multiple'],
            fulfill: {
              schema: {
                'x-hidden': '{{$deps[0] == false}}',
              },
            },
          },
        ],
        'x-component-props': {
          addonAfter: 'px',
          defaultValue: 200,
        }
      },
      defaultValueType: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'MemberDefaultValueType',
        'x-reactions': {
          dependencies: ['.tabs'],
          fulfill: {
            schema: {
              'x-component-props': {
                tabs: '{{$deps[0]}}',
              },
            },
          },
        },
      },
      memberDefaultValue: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'MemberDefaultValue',
        'x-reactions': {
          dependencies: ['.defaultValueType', '.multiple', '.tabs'],
          fulfill: {
            schema: {
              'x-component-props': {
                type: '{{$deps[0]}}',
                multiple: '{{$deps[1]}}',
                tabs: '{{$deps[2]}}',
              },
            },
          },
        },
      },
      // selectedType: {
      //   type: 'string',
      //   enum: ['name', 'nameUsername'],
      //   'x-decorator': 'FormItem',
      //   'x-component': 'Select',
      //   'x-component-props': {
      //     defaultValue: 'name',
      //   },
      // },
      pageSize: {
        type: 'number',
        'x-decorator': 'FormItem',
        'x-component': 'InputNumber',
        'x-component-props': {
          min: 1,
        },
      },
    }),
    ...createBizProps({
      collectivization: {
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
      },
      departmentFilteringRules: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'Select',
        enum: ['selected', 'exceptSelected'],
        'x-reactions': [
          {
            dependencies: ['form.attribute.x-component-biz-props.collectivization'],
            fulfill: {
              schema: {
                'x-hidden': '{{$deps[0] == true}}',
              },
            },
          },
        ],
        'x-component-props': {
          defaultValue: 'selected',
        },
      },
      departments: {
        type: 'Array',
        'x-decorator': 'FormItem',
        'x-component': 'MemberSelect',
        'x-component-props': {
          config: {
            tabs: [tabsData[0]],
            uniqueTabs: [tabsData[0]],
            topDepartmentIds: [1],
          },
          modalTitle: 'EformMemberSelect.lang.departments',
        },
        'x-reactions': [
          {
            dependencies: ['form.attribute.x-component-biz-props.collectivization'],
            fulfill: {
              schema: {
                'x-hidden': '{{$deps[0] == true}}',
              },
            },
          },
        ],
      },
      groupFilteringRules: {
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'Select',
        enum: ['selected', 'exceptSelected'],
        'x-reactions': [
          {
            dependencies: ['form.attribute.x-component-biz-props.collectivization'],
            fulfill: {
              schema: {
                'x-hidden': '{{$deps[0] == true}}',
              },
            },
          },
        ],
        'x-component-props': {
          defaultValue: 'selected',
        },
      },
      groups: {
        type: 'Array',
        'x-decorator': 'FormItem',
        'x-component': 'MemberSelect',
        'x-component-props': {
          config: {
            tabs: [tabsData[2]],
            uniqueTabs: [tabsData[2]],
          },
          modalTitle: 'EformMemberSelect.lang.groups',
          // type: 'group',
        },
        'x-reactions': [
          {
            dependencies: ['form.attribute.x-component-biz-props.collectivization'],
            fulfill: {
              schema: {
                'x-hidden': '{{$deps[0] == true}}',
              },
            },
          },
        ],
      },
    }),
    ...createValidatorSchema(false, {}),
  },
  style: {
    ...createComponentStyleSchema(),
    ...createComponentLayout(),
    ...createDecoratorStyleSchema(),
  },
  senior: {
    ...createModelSchema({
      childModelConfig: {
        type: 'boolean',
        'x-decorator': 'FormItem',
        'x-component': 'Switch',
      },
      storageConfig: {
        type: 'object',
        'x-decorator': 'FormItem',
        'x-component': 'StorageConfig',
        'x-component-props': {
          type: 'member',
        },
        'x-reactions': {
          dependencies: ['.childModelConfig'],
          fulfill: {
            state: {
              visible: "{{$deps == 'true'}}",
            },
          },
        },
      },
    }),
  },
  controller: createControllerSchema({
    events: [
      {
        name: "onOpen",
        label: "弹框打开事件"
      },
      {
        name: "onClose",
        label: "弹框关闭事件"
      },
      {
        name: "onSelect",
        label: "选中事件"
      },
      {
        name: "onChange",
        label: "点击确定按钮事件"
      },
      {
        name: "onBeforeOpen",
        label: "弹框打开前事件"
      },
      {
        name: "onBeforeClose",
        label: "弹框关闭前事件"
      },
      {
        name: "onBeforeChange",
        label: "组件值改变前事件"
      },
      {
        name: "onChange",
        label: "组件值改变前回调事件",
        isConfigEvent: true
      }]
  })
});
